package com.kqb.sgauth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.kqb.sgauth.wx.model.pojo.WxMiniProgramsResult;
import com.kqb.sgauth.wx.service.WxMiniProgramsService;
import com.kqb.sgcommon.satoken.StpKit;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杉果树皮
 */
@RestController
@Slf4j
@RequestMapping("/public/wx")
public class WxController {

    @Resource
    private WxMiniProgramsService wxMiniProgramsService;

    @GetMapping("/token")
    public String token(@RequestParam("code") String code) {
        WxMiniProgramsResult result = wxMiniProgramsService.getSessionKey(code);
        String sessionKey = result.getSession_key();
        String openid = result.getOpenid();
        StpKit.ADMIN.login(openid);
        String tokenValue = StpUtil.getTokenValue();
        System.out.println("tokenValue:" + tokenValue);

        return "当前会话是否登录：" + StpUtil.isLogin();
    }


    @GetMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }
}
